<?php
	require('classes/ShopExe.php');
	require('classes/ShopCategory.php');
	require('classes/ShopAttribute.php');
	require('classes/ShopProvider.php');
	require('classes/ShopShipping.php');
	require('funkcije.php');
	require('controllers/ShopAdmin.php');
	$ShopExe 			= new ShopExe();
	$ShopExeImpField 	= new ShopExeImpField();
	$ShopCat 			= new ShopCategory();
	$ShopAtt 			= new ShopAttribute();
	$ShopShipping 		= new ShopShipping($commander);
	$shopCom 			= new ShopAdmin();
	
	//$sections			= $menuAdmin->getProductSections(SITE_ID);
	
	$attributes 		= $ShopAtt->getShopAttributes();
	$exes				= $ShopExe->getShopExe();
	
	$products 			= $shopCom->getProducts();
	$shmethods 			= $ShopShipping->getShopShippingMethods();
	
	if($_REQUEST['shipping'] > 0)
	{ 
		$zones = $ShopShipping->getShopShippingZones($_REQUEST['shipping']); 
		$areas = $ShopShipping->getShopShippingAreas();
		if($_REQUEST['edit'] == 'rules')
		{ $rules = $ShopShipping->getShopShippingRules($_REQUEST['shipping']); }
	}
	
	if($_REQUEST['exe'] > 0)
	{ 
		
		if($shopCom->ShopSettings['provider'])
		{ 
			$ShopPro 			= new ShopProvider();
			$providers 			= $ShopPro->getShopProviders(); 
		}
		$brands = $shopCom->getProductBrands();
		$groups = $shopCom->getProductGroups();
		$cats 	= $ShopCat->getShopCats();
		
		$exe = $ShopExe->getShopExe($_REQUEST['exe']); 
		$eFields = $ShopExeImpField->getFields($_REQUEST['exe']);
		
		if($exe['type'] == 0)
		{
			if($_REQUEST['run'] == $exe['id'])
			{ $import_results = $ShopExe->runImport($exe, $eFields, $cats, $groups, $providers, $brands, $attributes, $siteAdmin, $shopCom, $ShopCat, $ShopPro, $ShopAtt); }
					
			if(is_file(PRODUCT_PATH.'import/csv/'.$exe['src_loc']))
			{
				$columns = 0;
				$row = 1;
				if($exe['def_row'] == 0)
				{ $minrow = $row; }
				else
				{ $minrow = $exe['def_row']; }
				$lines = count(file(PRODUCT_PATH.'import/csv/'.$exe['src_loc']));
				$handle = fopen(PRODUCT_PATH.'import/csv/'.$exe['src_loc'], "r");
				if($handle)
				{ 
					while (($row <= $minrow) && (($data = fgetcsv($handle, 1000, $exe['delimiter'])) !== FALSE)) 
					{
						if($data)
						{ $columns = count($data); }
						$row++;
					}
					fclose($handle);
				}
			}
		}
		
		if($exe['type'] == 1)
		{
			if($_REQUEST['run'] == $exe['id'])
			{ $import_results = $ShopExe->runImageImport($exe, $shopCom, $thumb); }
		}
	}
	
	if($_REQUEST['attribute'] > 0)
	{ 
		$attribute = $ShopAtt->getShopAttributes($_REQUEST['attribute']); 
		if(($attribute['id'] > 0) && ($attribute['predefined'] > 0))
		{ $attribute['values'] = $ShopAtt->getShopAttributeValues($attribute['id']); }
	}
	
	if($_REQUEST['item'] > 0)
	{ $item['id'] = $_REQUEST['item']; }
	if($item['id'] > 0)
	{ 
		$item = $shopCom->getProduct($item['id']); 
		//$item['attributes'] = $ShopAtt->getShopAttributes($item['id']);
		$item['vars'] = $shopCom->getProductVars($item['id'], 0, '');
	}
	
	switch ($_POST['drop_action'])
	{
		case 'move':
			if(($_POST['drag_item_id'] > 0) && ($_POST['drop_item_id'] > 0))
			{
				if(($_POST['drag_item_type'] == 'product') && ($_POST['drop_item_type'] == 'product_cat') && ($_POST['drag_from_id'] > 0))
				{ $shopCom->moveProductFromCatToCat($_POST['drag_item_id'], $_POST['drag_from_id'], $_POST['drop_item_id']); }
				
				if(($_POST['drag_item_type'] == 'product_cat') && ($_POST['drop_item_type'] == 'product_cat'))
				{ $shopCom->moveProductCat($_POST['drag_item_id'], $_POST['drop_item_id']); }
			}
			break;
	}
		
		
	switch ($_POST['action'])
	{
		case 'list_exe':
			if($_POST['new_exe'][0] != '')
			{ $ShopExe->addShopExe($_POST['new_exe'][0], $_POST['new_exe_type'][0]); }
			$i=0;
			if($_POST['exe_id'])
			{
				foreach($_POST['exe_id'] as $id)
				{
					$_POST['exe_title'][$i] = strip_tags(trim($_POST['exe_title'][$i]));
					$ShopExe->saveShopExe($id, $_POST['exe_title'][$i], $_POST['exe_type'][$i], $_POST['exe_frequency'][$i], $_POST['exe_timeset'][$i], $_POST['exe_source'][$i]);
					$i++;
				}
				
				if($_POST['activate'] > 0)
				{ $commander->setTableActive($_POST['activate'], 'shop_exe', 1); }
				if($_POST['deactivate'] > 0)
				{ $commander->setTableActive($_POST['deactivate'], 'shop_exe', 0); }
				
				if($_POST['autorun_0'] > 0)
				{ $commander->setTableFieldValueOnId('shop_exe', 'autorun', 0, $_POST['autorun_0']); }
				if($_POST['autorun_1'] > 0)
				{ $commander->setTableFieldValueOnId('shop_exe', 'autorun', 1, $_POST['autorun_1']); }
				
				if($_POST['delete_exe'] > 0)
				{ $ShopExe->deleteShopExe($_POST['delete_exe']); }
			}
			$exes	= $ShopExe->getShopExe();
			break;
		case 'editor_exe':
			if($exe['id'] > 0)
			{
				if(($_FILES['import_file']) && ($_FILES['import_file']['name'] != ''))
				{
					if ($_FILES['import_file']['error'] > 0)
					{ $error['import_file'] = "Error:" . $_FILES["file"]["error"] . "<br />"; }
					else
					{
						$temp_name = $_FILES['import_file']['name'];
						$extension = substr($temp_name,-3);
						$new_name = $exe['id'].'_'.date('Y-m-d-H-i-s').'.'.$extension;
						move_uploaded_file($_FILES['import_file']['tmp_name'], PRODUCT_PATH.'import/csv/'.$new_name);
						$info['import_file'] = 'Stored in: '.PRODUCT_PATH.'import/csv/'.$new_name;
						$ShopExe->saveShopExeSource($exe['id'], $_POST['src_type'], $_POST['src_loc_type'], $new_name);
					}
				}
				else
				{ 
					if($_POST['rem_imp_file'] > 0)
					{ 
						if(is_file(PRODUCT_PATH.'import/csv/'.$exe['src_loc']))
						{ unlink(PRODUCT_PATH.'import/csv/'.$exe['src_loc']); }
						$ShopExe->saveShopExeSource($exe['id'], $_POST['src_type'], $_POST['src_loc_type'], '');
					}
					else
					{
						if($exe['type'] != 1)
						{ $ShopExe->saveShopExeSource($exe['id'], $_POST['src_type'], $_POST['src_loc_type'], $exe['src_loc']); }
					}
				}
				
				if($_POST['src_loc_content'] != '')
				{ $ShopExe->saveShopExeSourceContent($exe['id'], $_POST['src_loc_content']); }
				
				if(($_POST['src_loc'] != '') && ($exe['type'] == 1))
				{ $ShopExe->saveShopExeSource($exe['id'], 0, 0, $_POST['src_loc']); }
				
				if($_POST['delimiter'])
				{ $ShopExe->saveShopExeFormat($exe['id'], $_POST['delimiter'], $_POST['def_row'], $_POST['start_row'], $_POST['target_root_id']); }
				
				if($_POST['from_field_no'])
				{
					$i=0;
					foreach($_POST['from_field_no'] as $field_no)
					{
						$ShopExeImpField->setFieldNo($field_no);
						$ShopExeImpField->setFieldTitle($_POST['from_field_title'][$i]);
						$ShopExeImpField->setToTable($_POST['to_table'][$i]);
						$ShopExeImpField->setToField($_POST['to_field'][$i]);
						$ShopExeImpField->setEmptyAction($_POST['if_empty_action'][$i]);
						$ShopExeImpField->setDefVal($_POST['default_value'][$i]);
						$ShopExeImpField->setPerfix($_POST['perfix'][$i]);
						$ShopExeImpField->setPostfix($_POST['postfix'][$i]);
						
						if($eFields[$i]['id'] > 0)
						{ $ShopExeImpField->saveField($eFields[$i]['id'], $eFields[$i]); }
						else
						{ $ShopExeImpField->addField($exe['id']); }
						$i++;
					}
				}
				
				$exe = $ShopExe->getShopExe($exe['id']);
				$eFields = $ShopExeImpField->getFields($exe['id']);
				if(is_file(PRODUCT_PATH.'import/csv/'.$exe['src_loc']))
				{
					$columns = 0;
					$row = 1;
					if($exe['def_row'] == 0)
					{ $minrow = $row; }
					else
					{ $minrow = $exe['def_row']; }
					$lines = count(file(PRODUCT_PATH.'import/csv/'.$exe['src_loc']));
					$handle = fopen(PRODUCT_PATH.'import/csv/'.$exe['src_loc'], "r");
					if($handle)
					{ 
						while (($row <= $minrow) && (($data = fgetcsv($handle, 1000, $exe['delimiter'])) !== FALSE)) 
						{
							if($data)
							{ $columns = count($data); }
							$row++;
						}
						fclose($handle);
					}
				}
			}
			break;	
		
		case 'list_shipping':
			if($_POST['new_shipping'][0] != '')
			{ $ShopShipping->addShopShipping($_POST['new_shipping'][0]); }
			$i=0;
			if($_POST['shipping_id'])
			{
				foreach($_POST['shipping_id'] as $id)
				{
					$ShopShipping->saveShopShipping($id, $_POST['shipping_title'][$i], $_POST['FreeShippingDateActiveStart'][$i], $_POST['FreeShippingDateActiveEnd'][$i]);
					$i++;
				}
				if($_POST['delete_shipping'] > 0)
				{ $ShopShipping->deleteShopShipping($_POST['delete_shipping']); }
			}
			$shmethods = $ShopShipping->getShopShippingMethods();
			break;
			
		case 'editor_shipping':
			if($_POST['new_zone'][0] != '')
			{ $ShopShipping->addShopShippingZone($commander->formatText($_POST['new_zone'][0]), $_REQUEST['shipping'], $_POST['new_zone_country'][0]); }
			$i=0;
			if($_POST['zone_id'])
			{
				foreach($_POST['zone_id'] as $id)
				{
					$ShopShipping->saveShopShippingZone($id, $commander->formatText($_POST['zone_title'][$i]), $_POST['zone_country'][$i], $_POST['zone_code'][$i], $_POST['zone_email'][$i]);
					if(($_POST['new_zip_start'][$i] > 0) || ($_POST['new_zip_end'][$i] > 0))
					{ $ShopShipping->addShopShippingArea($id, $_POST['new_city'][$i], $_POST['new_zip_start'][$i], $_POST['new_zip_end'][$i]); }
					$i++;
				}
				if($_POST['delete_zone'] > 0)
				{ $ShopShipping->deleteShopShippingZone($_POST['delete_zone']); }
			}
			
			$i=0;
			if($_POST['area_id'])
			{
				foreach($_POST['area_id'] as $id)
				{
					$ShopShipping->saveShopShippingArea($id, $_POST['area_city'][$i], $_POST['area_zip_start'][$i], $_POST['area_zip_end'][$i]);
					$i++;
				}
				if($_POST['delete_area'] > 0)
				{ $ShopShipping->deleteShopShippingArea($_POST['delete_area']); }
			}
			$zones = $ShopShipping->getShopShippingZones($_REQUEST['shipping']);
			$areas = $ShopShipping->getShopShippingAreas();
			break;
			
		case 'editor_shipping_rules':
			if($_POST['new_rule'][0] != '')
			{ $ShopShipping->addShopShippingRule($_REQUEST['shipping'], $_REQUEST['new_rule_zone'][0], $_REQUEST['new_rule'][0], $_REQUEST['new_rule_min_weight'][0], $_REQUEST['new_rule_max_weight'][0], $_REQUEST['new_rule_min_volume'][0], $_REQUEST['new_rule_max_volume'][0], $_REQUEST['new_rule_price'][0], $_REQUEST['new_rule_price_delivery_saturday'][0], $_REQUEST['new_rule_price_delivery_sunday'][0]);  }
			$i=0;
			if($_POST['rule_id'])
			{
				foreach($_POST['rule_id'] as $id)
				{
					$ShopShipping->saveShopShippingRule($id, $_REQUEST['rule_title'][$i], $_REQUEST['rule_zone'][$i], $_REQUEST['rule_min_weight'][$i], $_REQUEST['rule_max_weight'][$i], $_REQUEST['rule_min_volume'][$i], $_REQUEST['rule_max_volume'][$i], $_REQUEST['rule_price'][$i], $_REQUEST['rule_price_delivery_saturday'][$i], $_REQUEST['rule_price_delivery_sunday'][$i]);
					$i++;
				}
				if($_POST['delete_rule'] > 0)
				{ $ShopShipping->deleteShopShippingRule($_POST['delete_rule']); }
			}
			
			$rules = $ShopShipping->getShopShippingRules($_REQUEST['shipping']);
		case 'editor_product':
			//print_r($_POST);
			if($shop->CurrentProduct->Id > 0)
			{
				$item = $shopCom->getProduct($item['id']);
				$item['vars'] = $shopCom->getProductVars($item['id'], 0, '');
			}
			break;
			
		case 'editor_provider':
			if($_POST['add_brand'] > 0)
			{ $ShopPro->addBrandToProvider($provider['id'], $_POST['add_brand']);}
			if($_POST['rem_brand'] > 0)
			{ $ShopPro->remBrandToProvider($provider['id'], $_POST['rem_brand']);}
			
			break;
	}
	/*
	if($_REQUEST['provider'] > 0)
	{ 
		$provider 		= $ShopPro->getShopProvider($_REQUEST['provider']);
	}
	*/
	if($_REQUEST['shipping'] > 0)
	{ $zones = $ShopShipping->getShopShippingZones($_REQUEST['shipping']); }
	
	if($_REQUEST['cat'] > 0)
	{ $products = $shopCom->getProducts($_REQUEST['cat']); }
	else
	{ $products = $shopCom->getProducts(); }
	
?>